import { NextFunction, Request, Response } from "express";
import Base from "../../common/controller/base";
import { getUserInfo } from "../../utils/common";
import classSer from "./service";
/**
 * 班级管理相关
 */
class ClassController extends Base {
  /**
   * 加入班级
   */
  addClass = async (req: Request, res: Response, next: NextFunction) => {
    // 获取用户的id
    const { id } = getUserInfo(req);
    // 获取对应的班级id
    const classId = (req as any).classId;

    try {
      await classSer.addClass(id, classId);
      const data = await classSer.getStuClass(classId);
      this.success(res, { msg: "添加班级成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 将学生移除班级
   * @param req
   * @param res
   * @param next
   */
  removeClass = async (req: Request, res: Response, next: NextFunction) => {
    // 获取学生的id
    const { stuId, classId } = req.body;

    try {
      await classSer.removeClass(classId, stuId);
      this.success(res, { msg: "从班级移除学生成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 新建班级
   * @param req
   * @param res
   * @param next
   */
  createClass = async (req: Request, res: Response, next: NextFunction) => {
    const { name } = req.body;
    const { id } = getUserInfo(req);
    try {
      await classSer.createClass(name, id);
      this.success(res, { msg: "新建班级成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取所有的班级信息
   * @param req
   * @param res
   * @param next
   */
  getClass = async (req: Request, res: Response, next: NextFunction) => {
    const { pageNum = 1, pageSize = 10 } = req.query as any;
    const {id} = getUserInfo(req)
    try {
      const data = await classSer.getClass(pageSize, pageSize * (pageNum - 1), id);
      this.success(res, { msg: "获取班级信息成功", data: data.reverse() });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 删除班级
   * @param req
   * @param res
   * @param next
   */
  delClass = async (req: Request, res: Response, next: NextFunction) => {
    // 班级id
    const { classId } = req.body;
    // 得到老师的id
    const { id } = getUserInfo(req);
    try {
      await classSer.delClass(classId, id);
      this.success(res, { msg: "删除班级成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取一篇文章
   * @param req
   * @param res
   * @param next
   */
  getOne = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = req.params;

    try {
      const data = await classSer.getOne(id);
      this.success(res, { msg: "获取班级信息成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 编辑班级信息
   * @param req
   * @param res
   * @param next
   */
  editClas = async (req: Request, res: Response, next: NextFunction) => {
    const { id, name } = req.body;

    try {
      await classSer.editClass(id, name);
      this.success(res, { msg: "修改班级名字成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取学生的班级
   * @param req
   * @param res
   * @param next
   */
  getStuClass = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = getUserInfo(req);
    try {
      const data = await classSer.getStuClass(id);
      this.success(res, { msg: "获取班级信息成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 退出班级
   * @param req
   * @param res
   * @param next
   */
  exitStuClass = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = req.params;
    const { id: userId } = getUserInfo(req);
    try {
      const data = await classSer.exitStuClass(id, userId);
      this.success(res, { msg: "退出班级成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取教师首页信息
   * @param req
   * @param res
   * @param next
   */
  getTchInfo = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = getUserInfo(req);
    try {
      const data = await classSer.getTchInfo(id)
      this.success(res, { msg: "获取教师信息", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };
}

export default new ClassController();
